package edu.yctc.delivery.controller.rest.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import edu.yctc.delivery.constant.ResultCode;
import edu.yctc.delivery.controller.rest.MetadataRestController;
import edu.yctc.delivery.entity.DeliveryDO;
import edu.yctc.delivery.entity.DeliveryPointDO;
import edu.yctc.delivery.entity.ResultDO;
import edu.yctc.delivery.entity.TruckDO;
import edu.yctc.delivery.service.MetadataService;
import edu.yctc.delivery.vo.DeliveryVO;

/**
 * 元数据管理rest实现
 * 
 *
 * @author 渠立庆
 */
@RestController
@ComponentScan({"edu.yctc.delivery.service"})
@RequestMapping("/restmetadata")
public class MetadataRestControllerImpl implements MetadataRestController {

    private final static Logger LOG = LoggerFactory.getLogger("controllerLogger");

    @Autowired
    private MetadataService metadataService;

    @Override
    @PostMapping("get-deliveryvos-by-userid")
    public ResultDO<List<DeliveryVO>> getDeliveryVOsByUserId(@RequestBody long userId) {
        return metadataService.getDeliveryVOsByUserId(userId);
    }

    @Override
    @PostMapping("show-all-deliveryvos")
    public ResultDO<List<DeliveryVO>> showAllDelivery() {
        ResultDO<List<DeliveryDO>> resultDO = metadataService.getAllDeliveryDOs();
        if (resultDO.isSuccess() == false) {
            return new ResultDO<List<DeliveryVO>>(false, resultDO.getCode(), resultDO.getMsg(), null);
        }
        // 构建VO
        List<DeliveryDO> deliveryDOs = resultDO.getModule();
        List<DeliveryVO> deliveryVOs = new ArrayList<>();
        for (DeliveryDO deliveryDO : deliveryDOs) {
            DeliveryVO deliveryVO = metadataService.getDeliveryVOByDeliveryDO(deliveryDO).getModule();
            if (deliveryVO == null) {
                continue;
            }
            deliveryVOs.add(deliveryVO);
        }
        return new ResultDO<List<DeliveryVO>>(true, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS, deliveryVOs);
    }

    @Override
    @PostMapping("get-deliveryvos-by-username")
    public ResultDO<List<DeliveryVO>> getDeliveryVOsByName(String name) {
        ResultDO<List<DeliveryDO>> resultDO = metadataService.getDeliveryDOByUserName(name);
        if (resultDO.isSuccess() == false) {
            return new ResultDO<List<DeliveryVO>>(false, resultDO.getCode(), resultDO.getMsg(), null);
        }
        // 构建VO
        List<DeliveryDO> deliveryDOs = resultDO.getModule();
        List<DeliveryVO> deliveryVOs = new ArrayList<>();
        for (DeliveryDO deliveryDO : deliveryDOs) {
            DeliveryVO deliveryVO = metadataService.getDeliveryVOByDeliveryDO(deliveryDO).getModule();
            if (deliveryVO == null) {
                continue;
            }
            deliveryVOs.add(deliveryVO);
        }
        return new ResultDO<List<DeliveryVO>>(true, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS, deliveryVOs);
    }

    @Override
    @PostMapping("insert-deliverypoint")
    public ResultDO<Void> insertDeliveryPointDO(@RequestBody DeliveryPointDO deliveryPointDO) {
        // TODO 需求量有没有要求，后面添加到service中
        return metadataService.insertDeliveryPointDO(deliveryPointDO);
    }

    @Override
    @PostMapping("delete-deliverypoint-by-deliverypointid")
    public ResultDO<Void> deleteDeliveryPointDO(long deliveryPointId) {
        return metadataService.deleteDeliveryPointDO(deliveryPointId);
    }

    @Override
    @PostMapping("update-deliverypoint")
    public ResultDO<Void> updateDeliveryPointDO(@RequestBody DeliveryPointDO deliveryPointDO) {
        return metadataService.updateDeliveryPointDO(deliveryPointDO);
    }

    @Override
    @PostMapping("insert-truck")
    public ResultDO<Void> insertTruckDO(@RequestBody TruckDO truckDO) {
        return metadataService.insertTruckDO(truckDO);
    }

    @Override
    @PostMapping("delete-truck-by-truckid")
    public ResultDO<Void> deleteTruckDO(long truckId) {
        return metadataService.deleteTruckDO(truckId);
    }

    @Override
    @PostMapping("update-truck")
    public ResultDO<Void> updateTruckDO(@RequestBody TruckDO truckDO) {
        return metadataService.updateTruckDO(truckDO);
    }

    @Override
    @PostMapping("get-deliverypoint-by-deliverypointid")
    public ResultDO<DeliveryPointDO> getDeliveryPointDOById(long deliveryPointId) {
        return metadataService.getDeliveryPointDOById(deliveryPointId);
    }

    @Override
    @PostMapping("get-truck-by-truckid")
    public ResultDO<TruckDO> getTruckDOById(long truckId) {
        return metadataService.getTruckDOById(truckId);
    }

    @Override
    @PostMapping("delete-delivery-by-userid")
    public ResultDO<Void> deleteDeliveryDOsByUserId(long userId) {
        return metadataService.deleteDeliveryDOsByUserId(userId);
    }

    @Override
    @PostMapping("delete-delivery-by-userid-and-deliverypointid")
    public ResultDO<Void> deleteDeliveryDOByUserIdAndDeliveryPointId(long userId, long deliveryPointId) {
        return metadataService.deleteDeliveryDOByUserIdAndDeliveryPointId(userId, deliveryPointId);
    }

    @Override
    @PostMapping("insert-deliverys")
    public ResultDO<Void> insertDeliveryDOs(@RequestBody List<DeliveryVO> deliveryVOs) {
        if (deliveryVOs.size() > 0) {
            return metadataService.insertDeliveryDOs(deliveryVOs);
        } else {
            LOG.error("insert deliveryDOs fail, parameter no deliverys to insert, deliveryVOs={}", deliveryVOs);
            return new ResultDO<>(false, ResultCode.NO_DELIVERYS_TO_INSERT, ResultCode.MSG_NO_DELIVERYS_TO_INSERT);
        }
    }

}
